package collection;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class CollectionSortDemo03 {
    public static void main(String[] args) {
        Point p1 = new Point(10,20);
        Point p2 = new Point(5,30);
        List<Point> list = new ArrayList<>();
        list.add(p1);
        list.add(p2);
        //调用Collections,sort(List<T> list,Comparator cc)方法对Point对象排序
        //方法中的Comparator对象为比较器对象，是用于更加灵活的排序场景
        Comparator<Point> comparator1 = new Comparator<Point>(){
            public int compare(Point o1,Point o2){
                return o1.getX() - o2.getX();
            }
        };
        Comparator<Point> comparator2 = new Comparator<Point>(){
            public int compare(Point o1,Point o2){
                return o1.getY() - o2.getY();
            }
        };
        Collections.sort(list,comparator1);
        System.out.println(list);
        Collections.sort(list,new Comparator<Point>(){
            public int compare(Point o1,Point o2){
                return o1.getX() - o2.getX();
            }
        });
        System.out.println(list);
    }
}
